package com.lzb.newloan.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzb.newloan.ProblemActivity;
import com.lzb.newloan.R;
import com.lzb.newloan.pojo.MarketPojo;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

public class MarketAdapter extends BaseAdapter {

	private ArrayList<MarketPojo> pojo;
	private LayoutInflater mInflater;
	private Context context;

	public MarketAdapter(Context context, ArrayList<MarketPojo> pojo) {
		this.pojo = pojo;
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public ArrayList<MarketPojo> getArrayList() {
		return pojo;
	}

	public void setArrayList(ArrayList<MarketPojo> arrayList) {
		this.pojo = arrayList;
	}

	@Override
	public int getCount() {
		if (pojo == null) {
			return 0;
		} else {
			return pojo.size();
		}
	}

	@Override
	public Object getItem(int arg0) {

		return pojo.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int postition, View convertView, ViewGroup arg2) {
		ViewHoler viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHoler();
			convertView = mInflater.inflate(R.layout.adapter_market, null);
			viewHolder.logo = (ImageView) convertView.findViewById(R.id.logo);
			viewHolder.title = (TextView) convertView.findViewById(R.id.title);
			viewHolder.money_number = (TextView) convertView
					.findViewById(R.id.money_number);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHoler) convertView.getTag();
		}
		final MarketPojo data = pojo.get(postition);
		ImageLoader.getInstance().displayImage(data.getLogo(),
				viewHolder.logo);
		viewHolder.title.setText(data.getTitle());
		viewHolder.money_number.setText(data.getAdvantage());
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, ProblemActivity.class);
				intent.putExtra("title", data.getTitle());
				intent.putExtra("url",data.getUrl());
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	static class ViewHoler {
		public ImageView logo;
		public TextView title, money, money_number;
	}

}
